package ntrees.hu.pxinput;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.CornerPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.Log;
import android.view.View;

public class ArcView extends View {

	/**
	 * 弧度的半径
	 * */
	private float radius;
	protected Context mContext;
	/**
	 * 弧形的宽度*/
	private int width;
	/**
	 * 弧形背景*/
	protected /*static*/ Paint paint;

	/**
	 * 中心位置对于的角度*/
	private float angle;
	private float sweepAngle;
	
	/**
	 * 圆心*/
	Position center;
//	private int center_x = -1;
//	private int center_y = -1;
	
	static int colors[] = {
			0x88ff0000,
			0x8800ff00,
			0x880000ff,
			0x88ffff00,
			0x88ff00ff,
			0x8800ffff
	};
	
	static int i = 0;
	
//	static {
//    	paint = new Paint(Paint.ANTI_ALIAS_FLAG);
//    	paint.setColor(colors[i++%6]);
//    	paint.setStyle(Paint.Style.STROKE);
//    	paint.setStrokeWidth(32);
//    	paint.setPathEffect(new CornerPathEffect(10));
//	}
	
	public ArcView(Context context) {
		super(context);
		mContext = context;
    	paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    	paint.setColor(/*colors[i++%6]*/Color.BLUE);
    	paint.setStyle(Paint.Style.STROKE);
    	paint.setStrokeWidth(32);
    	paint.setPathEffect(new CornerPathEffect(10));
	}
	
	public void setCenter(float x, float y) {
		center = new Position(x, y);
	}
	
	public Position center() {
		return center;
	}
	
	public void onDraw(Canvas canvas) {
		//canvas.drawColor(Color.GRAY);
		if (paint == null) {
			paint = new Paint();
		}
		Path path = new Path();
		path.addArc(rect(), startAngle(), sweepAngle());
    	canvas.drawPath(path, paint);
	}
	
	protected RectF rect() {
		dump("rect");
		return new RectF((float) (center().x - radius()), 
				(float) (center().y - radius()), 
				(float) (center().x + radius()), 
				(float) (center().y + radius()));
	}
	
	public void setRadius(float radius) {
		this.radius = radius;
	}
	
	public float radius() {
		return radius;
	}
	
	public void setWidth(int width) {
		this.width = width;
	}
	
	public int width() {
		return width;
	}
	
	public void setSweepAngle(float sweepAngle) {
		this.sweepAngle = sweepAngle;
	}
	
	public float sweepAngle() {
		return sweepAngle;
	}
	
	public void setAngle(float midAngle) {
		this.angle = midAngle;
	}
	
	public float angle() {
		return angle;
	}
	
	protected float startAngle() {
		return angle() - (sweepAngle()/2);
	}
	
	public void dump(String tag) {
		Log.d(tag,
				"{sweepAngle: " + sweepAngle() + "}"
				 + "{angle: " + angle() + "}"
				 + "{width: " + width() + "}"
				 + "{radius: " + radius() + "}"
				 + "{center: " + center() + "}"
				);
	}
}
